package com.jxc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.CustomerType;
import com.jxc.model.po.Member;
import com.jxc.model.po.MemberType;
import com.jxc.model.po.User;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.VMember;
import com.jxc.service.MemberServiceI;

@Service("memberService")
public class MemberServiceImpl implements MemberServiceI {
	@Autowired
	private BaseDaoI<Member> memberDao;

	@Override
	public List<VMember> combobox(VMember vmember) {
		return this.changeModel(this.find(vmember));
	}

	@Override
	public DataGrid datagrid(VMember vMember) {
		DataGrid j = new DataGrid();
		j.setRows(this.changeModel(this.find(vMember)));
		j.setTotal(this.total(vMember));
		return j;
	}

	public DataGrid datagridTotal(VMember vMember) {
		DataGrid j = new DataGrid();
		j.setRows(this.changeModel(this.findTotal(vMember)));
		//j.setTotal(this.totalTotal(vMember));
		return j;
	}
	
	private List<Member> find(VMember vMember) {
		String hql = "from Member t where t.isDeleted=false ";

		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vMember, hql, values);

		if (vMember.getSort() != null && vMember.getOrder() != null) {
			hql += " order by " + vMember.getSort() + " " + vMember.getOrder();
		}
		return memberDao.find(hql, values, vMember.getPage(), vMember.getRows());
	}

	private List<Member> findTotal(VMember vMember) {
		String hql = "from Member t where t.isDeleted=false ";

		List<Object> values = new ArrayList<Object>();
		hql = addWhereTotal(vMember, hql, values);

		if (vMember.getSort() != null && vMember.getOrder() != null) {
			hql += " order by " + vMember.getSort() + " " + vMember.getOrder();
		}
		return memberDao.find(hql, values, vMember.getPage(), vMember.getRows());
	}
	
	private Long totalTotal(VMember vMember) {
		String hql = "select count(*) from Member t where t.isDeleted=false ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhereTotal(vMember, hql, values);
		return memberDao.count(hql, values);
	}
	
	private List<VMember> changeModel(List<Member> members) {
		List<VMember> vMemberes = new ArrayList<VMember>();
		if (members != null && members.size() > 0) {
			for (Member m : members) {
				VMember vMember = new VMember();
				BeanUtils.copyProperties(m, vMember);
				if (m.getMemberType() != null) {
					MemberType type = m.getMemberType();
					vMember.setTid(type.getId());
					vMember.setTname(type.getName());
				}
				if (m.getCustomerType() != null) {
					CustomerType ct = m.getCustomerType();
					vMember.setCtId(ct.getId());
					vMember.setCtName(ct.getName());
				}
				if (m.getUser() != null) {
					User user = m.getUser();
					vMember.setUid(user.getId());
					vMember.setUname(user.getUsername());
				}
				vMemberes.add(vMember);
			}
		}
		return vMemberes;
	}

	private Long total(VMember vMember) {
		String hql = "select count(*) from Member t where t.isDeleted=false ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vMember, hql, values);
		return memberDao.count(hql, values);
	}

	private String addWhere(VMember vMember, String hql, List<Object> values) {
		if (vMember.getTid() != null) {
			hql += " and t.memberType.id=? ";
			values.add(vMember.getTid());
		}
		if (vMember.getSerial() != null) {
			hql += " and t.serial = ? ";
			values.add(vMember.getSerial());
		}
		if (vMember.getCtId() != null) {
			hql += " and t.customerType = ? ";
			values.add(vMember.getCtId());
		}
		if (vMember.getMobile() != null) {
			hql += " and t.mobile = ? ";
			values.add(vMember.getMobile());
		}
		return hql;
	}
	
	private String addWhereTotal(VMember vMember, String hql, List<Object> values) {
		if (vMember.getUid() != null) {
			hql += " and t.user.id = ? ";
			values.add(vMember.getUid());
		}
		if (vMember.getConsumeStart() != null && vMember.getConsumeEnd() != null) {
			hql += " and t.consume >= ? and t.consume <= ? ";
			values.add(vMember.getConsumeStart());
			values.add(vMember.getConsumeEnd());
		}

		if (vMember.getPointStart() != null && vMember.getPointEnd() != null) {
			hql += " and t.point >= ? and t.point <= ? ";
			values.add(vMember.getPointStart());
			values.add(vMember.getPointEnd());
		}		
		return hql;
	}

	@Override
	public void delete(String ids) {
		if (ids != null) {
			for (String id : ids.split(",")) {
				if (!id.trim().equals("0")) {
					Member m = memberDao.get(Member.class, Long.valueOf(id.trim()));
					if (m != null) {
						m.setIsDeleted(true);
						memberDao.update(m);
					}
				}
			}
		}

	}

	@Override
	public Long save(VMember vMember) {
		Member m = new Member();
		BeanUtils.copyProperties(vMember, m);
		if (vMember.getTid() != null) {
			MemberType type = new MemberType();
			type.setId(vMember.getTid());
			m.setMemberType(type);
		}
		if (vMember.getUid() != null) {
			User user = new User();
			user.setId(vMember.getUid());
			m.setUser(user);
		}
		if (vMember.getCtId() != null) {
			CustomerType ct = new CustomerType();
			ct.setId(vMember.getCtId());
			m.setCustomerType(ct);
		}
		m.setIsDeleted(false);
		memberDao.save(m);
		return m.getId();
	}

	@Override
	public void update(VMember vMember) {
		Member m = memberDao.get(Member.class, vMember.getId());
		BeanUtils.copyProperties(vMember, m);
		if (vMember.getTid() != null) {
			MemberType type = new MemberType();
			type.setId(vMember.getTid());
			m.setMemberType(type);
		}
		if (vMember.getUid() != null) {
			User user = new User();
			user.setId(vMember.getUid());
			m.setUser(user);
		}
		if (vMember.getCtId() != null) {
			CustomerType ct = new CustomerType();
			ct.setId(vMember.getCtId());
			m.setCustomerType(ct);
		}
		m.setIsDeleted(false);
		memberDao.update(m);

	}

	@Override
	public Member getMemberById(Long id) {
		return memberDao.get(Member.class, id);
	}

}
